package controllers;

import beans.InhoudBean1;
import connectivity.Dbmanager;
import connectivity.QueryManager;
import java.io.*;
import java.util.LinkedList;
import javax.servlet.*;
import javax.servlet.http.*;
import models.Course;
import models.Module;
import tools.Checker;
import models.CombiDeal;

public class ChangeCombidealController extends HttpServlet {

    private Dbmanager dbManager;
    private QueryManager queryManager;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        dbManager = new Dbmanager();
        dbManager.openConnection();
        queryManager = new QueryManager(dbManager);
        CombiDeal combideal;
        LinkedList<Course> allcourses = queryManager.getAllCourses();
        request.setAttribute("allcourses", allcourses);

        if (request.getParameter("deletecombi") != null) {
            int ID = Integer.parseInt(request.getParameter("deletecombi"));
            queryManager.deletecombidealbyid(ID);
            dbManager.closeConnection();
            response.sendRedirect("combination_deals?managecombi=31");
        } else if (request.getParameter("newcombideal") != null) {
            combideal = new CombiDeal(0, 0, 0, 0);
            combideal.setCombidealprice(0.0);
            request.setAttribute("newcombi", 1);
            request.setAttribute("combideal", combideal);
            dbManager.closeConnection();
            RequestDispatcher dispatcher = request.getRequestDispatcher("/change_combideal.jsp");
            dispatcher.forward(request, response);
        } else {
            int combiID = Integer.parseInt(request.getParameter("combiID"));
            combideal = queryManager.getCombiDealbyid(combiID);
            combideal.setCombidealid(combiID);
            request.setAttribute("combideal", combideal);


            dbManager.closeConnection();
            RequestDispatcher dispatcher = request.getRequestDispatcher("/change_combideal.jsp");
            dispatcher.forward(request, response);
        }



    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        dbManager = new Dbmanager();
        dbManager.openConnection();
        queryManager = new QueryManager(dbManager);
        int course_1 = 0, course_2 = 0, course_3 = 0, course_4 = 0, combidealID = 0;
        double combidealprice = 0.0;
        String address = "";
        boolean isEmpty = false;
        CombiDeal combideal = null;
        HttpSession sessie = request.getSession();
        InhoudBean1 inhoud = (InhoudBean1) sessie.getAttribute("inhoud");




        if (!request.getParameter("course1").isEmpty() && request.getParameter("course1") != "0") {
            course_1 = Integer.parseInt(request.getParameter("course1"));
        } else {
            isEmpty = true;
            request.setAttribute("foutMelding", inhoud.getError19());
        }
        if (!request.getParameter("course2").isEmpty() && request.getParameter("course1") != "0") {
            course_2 = Integer.parseInt(request.getParameter("course2"));
        } else {
            isEmpty = true;
            request.setAttribute("foutMelding", inhoud.getError19());
        }

        if (!request.getParameter("course3").isEmpty()) {
            course_3 = Integer.parseInt(request.getParameter("course3"));
        } else {
            course_3 = 0;
        }
        if (!request.getParameter("course4").isEmpty()) {
            course_4 = Integer.parseInt(request.getParameter("course4"));
        } else {
            course_4 = 0;
        }
        
        if (!request.getParameter("combidealprice").isEmpty()) {
                Checker checker = new Checker();
                String priceS = request.getParameter("combidealprice"), priceS2 = "";
                for (int i = 0; i < priceS.length(); i++) {
                    char temp;
                    temp = priceS.charAt(i);
                    if (temp == ',') {
                        temp = '.';
                    }
                    priceS2 = priceS2 + temp;
                }
                if (checker.isDigit(priceS2)) {
                    combidealprice = Double.parseDouble(priceS2);
                } else {
                    sessie.setAttribute("foutMelding", inhoud.getError5());
                    isEmpty = true;
                }
            } else {
                isEmpty = true;
                sessie.setAttribute("foutMelding", inhoud.getError19());
            }

        combideal = new CombiDeal(course_1, course_2, course_3, course_4);
        combideal.setCombidealprice(combidealprice);



        if (!isEmpty) {

            if (request.getParameter("combidealid") != null) {
                combidealID = Integer.parseInt(request.getParameter("combidealid"));
                combideal.setCombidealid(combidealID);
                queryManager.updateCombiDeal(combideal);
            } else {
                queryManager.addcombideal(combideal);
            }

            dbManager.closeConnection();
            address = "combination_deals?managecombi=31";
            response.sendRedirect(address);
        } else {
            LinkedList<Course> allcourses = queryManager.getAllCourses();
            request.setAttribute("allcourses", allcourses);
            request.setAttribute("combideal", combideal);
            address = "/change_combideal.jsp";

            dbManager.closeConnection();
            RequestDispatcher dispatcher = request.getRequestDispatcher(address);

            dispatcher.forward(request, response);

        }


    }
}
